﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace P2PLib.Client.Command
{
    public class CCSearchCommand:CCCommandBase
    {
        private SearchType searchType;
        private string key;
        public CCSearchCommand() 
        :base(CCCommandType.Search)
        {
            this.searchType= SearchType.ByHashCode;
            this.key = string.Empty;
            
        }
        public CCSearchCommand(SearchType st, string k)
            : base(CCCommandType.Search)
        {
            this.searchType = st;
            this.key = k;
        }
        public SearchType SearchType {
            get {
                return this.searchType;
            }
            set {
                this.searchType = value;
            }
        }
        public string Key {
            get {
                return this.key;
            }
            set {
                this.key = value;
            }
        }
        public override void ReadFromStream(System.IO.Stream stream)
        {
            byte[] buffer = new byte[4];
            stream.Read(buffer, 0, 4);
            int t = BitConverter.ToInt32(buffer, 0);
            this.searchType = (SearchType)t;
            stream.Read(buffer, 0, 4);
            int size = BitConverter.ToInt32(buffer, 0);
            buffer = new byte[size];
            stream.Read(buffer, 0, size);
            this.key = Encoding.UTF8.GetString(buffer);
        }
        public override void WriteToStream(System.IO.Stream stream)
        {
            byte[] buffer = new byte[4];
            buffer = BitConverter.GetBytes((int) this.searchType);
            stream.Write(buffer, 0, 4);
            buffer = Encoding.UTF8.GetBytes(this.key);
            stream.Write(BitConverter.GetBytes(buffer.Length), 0, 4);
            stream.Write(buffer, 0, buffer.Length);
        }
    }
}
